/*
 * Copyright (c) 2010, Jim Hollinger
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of Jim Hollinger nor the names of its contributors
 *     may be used to endorse or promote products derived from this
 *     software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#ifndef WPSJGP_WPS_INTERFACE_H
#define WPSJGP_WPS_INTERFACE_H

#include <string>
#include "wpsapi.h"
#include "pthread.h"

namespace kmlio {
	class KmlWriter;
};

namespace wpsjgp {

class WpsStatus;

class WpsInterface {
public:
	friend static WPS_Continuation wps_periodic_callback(void *, WPS_ReturnCode, const WPS_Location *, const void *);
	friend static WPS_Continuation wps_tiling_callback(void *, unsigned int, unsigned int);
	friend static void *wps_start_pthread(void *);

public:
	WpsInterface();
	~WpsInterface();

	bool init(const std::string &username, const std::string &realm, const std::string &tiles_directory, const std::string &kml_filename);
	bool start(double interfaceRate_hz);
	void stop();
	bool isStop() const;
	void getStaus(WpsStatus *dst_status);

	static const char *WPS_ErrorToString(int);

protected:
	bool run();
	void newLocation(double latitude_degN, double longitude_degE, double altitude_m, double bearing_rad, double speed_mps, double horizontalError_m,
		int numAccessPoints, int numCellTowers, int numSatellites, int status_code, const char *status_str);
	void setErrorCode(int error_code, const char *error_str);
	void setTileDownloads(unsigned int tilesDownloaded, unsigned int tilesToDownload);

public:
	static const unsigned int   tilesTotalDataSize;

protected:
	pthread_t                   thread_ID;
	pthread_mutex_t             status_mutex;
	WpsStatus                  *status;

	void                       *my_authentication;

	kmlio::KmlWriter           *kmlWriter;

	unsigned long               period_ms;

	volatile bool               stop_flag;
	volatile unsigned int       tilesToDownload;
	volatile unsigned int       tilesDownloaded;
};

} /* namespace wpsjgp */

#endif /* WPSJGP_WPS_INTERFACE_H */
